package com.lzx.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.payment.entity.OrderInfo;
import com.lzx.payment.entity.vo.Activity;
import com.lzx.payment.enums.OrderStatus;
import com.lzx.payment.feign.ActivityClient;
import com.lzx.payment.mapper.OrderInfoMapper;
import com.lzx.payment.service.OrderInfoService;
import com.lzx.payment.util.OrderNoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2022-12-14
 */
@Slf4j
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Resource
    private ActivityClient activityClient;

    //生成订单
    @Override
    public OrderInfo createOrderByProductId(Long activityId, Long userId, String paymentType) {
        //查找已存在但未支付的订单
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", activityId);
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("order_status", OrderStatus.NOTPAY.getType());
        queryWrapper.eq("payment_type", paymentType);
        OrderInfo orderInfo = orderInfoMapper.selectOne(queryWrapper);
        if (orderInfo != null) {
            return orderInfo;
        }
        //获取商品信息
        Activity activity = activityClient.getActivityById(activityId);
        //生成订单
        orderInfo = new OrderInfo();
        orderInfo.setTitle(activity.getName());
        orderInfo.setOrderNo(OrderNoUtils.getOrderNo()); //订单号
        orderInfo.setUserId(userId);
        orderInfo.setProductId(activityId);
        orderInfo.setTotalFee(activity.getPrice()); //分
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType()); //未支付
        orderInfo.setPaymentType((paymentType));
        baseMapper.insert(orderInfo);
        return orderInfo;
    }

    //根据订单号查询订单
    @Override
    public OrderInfo getOrderByOrderNo(String outTradeNo) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", outTradeNo);
        return baseMapper.selectOne(queryWrapper);
    }

    //根据订单号更新订单状态
    @Override
    public void updateStatusByOrderNo(String orderNo, OrderStatus orderStatus) {
        log.info("更新订单状态 ===> {}", orderStatus.getType());
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderStatus(orderStatus.getType());
        baseMapper.update(orderInfo, queryWrapper);
    }

    //根据订单号获取订单状态
    @Override
    public String getOrderStatus(String orderNo) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        if (orderInfo == null) {
            return null;
        }
        return orderInfo.getOrderStatus();
    }

    //查询创建超过minutes分钟并且未支付的订单
    @Override
    public List<OrderInfo> getNoPayOrderByDuration(int minutes, String paymentType) {
        //五分钟之前的时间
        Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status", OrderStatus.NOTPAY.getType());
        queryWrapper.le("create_time", instant);
        queryWrapper.eq("payment_type", paymentType);
        return orderInfoMapper.selectList(queryWrapper);
    }
}
